<?php

class ServicesController extends Controller
{
	var $pageTitle = 'Автодинамика - Услуги';
	var $_model;
	var $page_id = 'page2';

	public function actionIndex()
	{
	
		$part = Part::model()->find('alias=:alias', array(':alias'=>'services'));
		if(!empty($part->page_title))
			$this->pageTitle = $part->page_title;
		
		if(!empty($part->page_description))		
			$this->pageDescription = $part->page_description;
			
		if(!empty($part->page_keywords))
			$this->pageKeyWords = $part->page_keywords;
	
		$criteria=new CDbCriteria(array(
			'condition'=>'part_id='.Part::getByAlias('services')->id,
			'order'=>'title',
		));
	 
		$dataProvider=new CActiveDataProvider('Page', array(
			'pagination'=>false,
			'criteria'=>$criteria,
		));
		
		$criteria1=new CDbCriteria(array(
			'condition'=>'part_id='.Part::getByAlias('offers')->id,
			'order'=>'title',
		));
	 
		$offers= Page::model()->findAll($criteria1);
	 
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
			'offers'=>$offers,
		));
	}
	
	public function actionView(){
		
		$model = $this->loadModel();
		
		if(!empty($model->page_title))
			$this->pageTitle = $model->page_title;
		else
			$this->pageTitle=Yii::app()->name . ' - Услуги - '.CHtml::encode($model->title);
		
		if(!empty($model->page_description))		
			$this->pageDescription = $model->page_description;
		else
			$this->pageDescription=CHtml::encode($model->preamble);
			
		if(!empty($model->page_keywords))
			$this->pageKeyWords = $model->page_keywords;
		else
			$this->pageKeyWords=CHtml::encode(implode(' ,', CHtml::listData($model->tags, 'tag', 'tag')));
		
		$this->render('view', array('model'=>$model));
	}
	
	private function loadModel()
	{
		if($this->_model===null)
		{
			if(isset($_GET['alias']))
				$this->_model=Page::model()->find('alias=:alias', array(':alias'=>$_GET['alias']));
			if($this->_model===null)
				throw new CHttpException(404,'The requested page does not exist.');
		}
		return $this->_model;
	}
}